package com.h2.ref.server.auth;

import java.util.Collection;

import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;

public class AuthUtils {

   public static void changeAuth(String username, String password) {
      SecurityContext context = SecurityContextHolder.getContext();
      if(context != null) {
         
         Collection<? extends GrantedAuthority> authorities = SecurityContextHolder
               .getContext().getAuthentication().getAuthorities();

         //clear the context
         SecurityContextHolder.clearContext();
         
         //create new token
         UsernamePasswordAuthenticationToken auth = 
            new UsernamePasswordAuthenticationToken(username, password,
                  authorities);
         
         //set new auth token
         SecurityContextHolder.getContext().setAuthentication(auth);
      }
   }
}
